package gases.as3.util
{
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class ResizeUtil
	{
		public function ResizeUtil()
		{
		}
		
		/**
		 * 对齐
		 * 
		 * @param obj
		 * @param container
		 * @param horizontalAlign
		 * @param verticalAlign
		 * 
		 */
		public static function silder(obj:*,container:*,horizontalAlign:String = null,verticalAlign:String = null):void
		{
			var cRect:Rectangle = getRect(container,container);
			var rect:Rectangle = getRect(obj,container);
			var offest:Point = new Point(obj.x - rect.x,obj.y - rect.y);
			switch (horizontalAlign)
			{
				case ResizeAlignMode.LEFT:
					obj.x = cRect.x + offest.x;
					break;
				case ResizeAlignMode.CENTER:
					obj.x = cRect.x + (cRect.width - rect.width)/2 + offest.x;
					break;
				case ResizeAlignMode.RIGHT:
					obj.x = cRect.x + (cRect.width - rect.width) + offest.x;
					break;
			}
			switch (verticalAlign)
			{
				case ResizeAlignMode.TOP:
					obj.y = cRect.y + offest.y;
					break;
				case ResizeAlignMode.MIDDLE:
					obj.y = cRect.y + (cRect.height - rect.height)/2 + offest.y;
					break;
				case ResizeAlignMode.BOTTOM:
					obj.y = cRect.y + (cRect.height - rect.height) + offest.y;
					break;
			}
		}
		
		/**
		 * 从中部偏移
		 * 
		 * @param obj
		 * @param container
		 * @param horizontalCenter
		 * @param verticalCenter
		 * 
		 */
		public static function center(obj:*,container:*,horizontalCenter:Number = NaN,verticalCenter:Number = NaN):void
		{
			var cRect:Rectangle = getRect(container,container);
			var rect:Rectangle = getRect(obj,container);
			var offest:Point = new Point(obj.x - rect.x,obj.y - rect.y);
			
			if (!isNaN(horizontalCenter))
				obj.x = cRect.x + (cRect.width - rect.width)/2 + offest.x + horizontalCenter;
			
			if (!isNaN(verticalCenter))
				obj.y = cRect.y + (cRect.height - rect.height)/2 + offest.y + verticalCenter;
			
		}
		
		/**
		 * 获得矩形（以父容器为基准）
		 * 当源是stage时，获取的将不是图形矩形，而是舞台的矩形。必要的时候请用root代替。
		 * 
		 * @param obj	显示对象或者矩形，矩形将会被直接返回
		 * @param targetSpace	当前坐标系，默认值为父显示对象
		 * @return 
		 * 
		 */		
		public static function getRect(obj:*,space:DisplayObject=null):Rectangle
		{	
			if (!obj)
				return null;
			
			if (obj is Rectangle)
				return (obj as Rectangle).clone();
			
			if (obj is Stage)
			{
				var stageRect:Rectangle = new Rectangle(0,0,(obj as Stage).stageWidth,(obj as Stage).stageHeight);//目标为舞台则取舞台矩形
				if (space)
					return localRectToContent(stageRect,obj as DisplayObject,space);
				else
					return stageRect;
			}
			
			if (obj is DisplayObject)
			{
				if (!space)
					space = (obj as DisplayObject).parent;
				
				if (obj.width == 0 || obj.height == 0)//长框为0则只变换坐标
				{
					var p:Point = localToContent(new Point(),obj,space);
					return new Rectangle(p.x,p.y,0,0);
				}
				
				if ((obj as DisplayObject).scrollRect)//scrollRect有时候不会立即确认属性
					return localRectToContent((obj as DisplayObject).scrollRect,obj,space)
				else
					
					return  (obj as DisplayObject).getRect(space);
			}
			
			return null;
		}
		
		/**
		 * 获得注册点（相对与显示出的图形）
		 * @param v
		 * @return 
		 * 
		 */
		public static function getRegPoint(v:DisplayObject,space:DisplayObject=null):Point
		{
			if (!space)
				space = v;
			
			return v.getRect(space).topLeft;
		}
		
		/**
		 * 转换坐标到某个显示对象
		 * 
		 * @param pos	坐标
		 * @param source	源
		 * @param target	目标
		 * @return 
		 * 
		 */		
		public static function localToContent(pos:Point,source:DisplayObject,target:DisplayObject):Point
		{
			if (target && source)
				return target.globalToLocal(source.localToGlobal(pos));
			else if (source)
				return source.localToGlobal(pos);
			else if (target)
				return target.globalToLocal(pos);
			return null;
		}
		
		/**
		 * 转换矩形坐标到某个显示对象
		 * 
		 * @param rect	矩形
		 * @param source	源
		 * @param target	目标
		 * @return 
		 * 
		 */		
		public static function localRectToContent(rect:Rectangle,source:DisplayObject,target:DisplayObject):Rectangle
		{
			if (source == target)
				return rect;
			
			var topLeft:Point = localToContent(rect.topLeft,source,target);
			var bottomRight:Point = localToContent(rect.bottomRight,source,target);
			return new Rectangle(topLeft.x,topLeft.y,bottomRight.x - topLeft.x,bottomRight.y - topLeft.y);
		}
		
	}
}